//
#import "WebviewViewController.h"

@interface WebviewViewController ()

@end

@implementation WebviewViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _backButton = [self resourceBasedButton:@"ic_arrow_left" withAction:@selector(onBack:)];
    [[self navigationItem] setLeftBarButtonItem:_backButton];

    if (_idDrugs > -1) {
        NSString *contentHtml = [self getData:_idDrugs];
        if(_idDrugs > 2)
        {
            NSRange range = [contentHtml rangeOfString:@"<body>" options:NSBackwardsSearch range:NSMakeRange(0, 600)];
            if (range.location != NSNotFound) {
                contentHtml =  [contentHtml stringByReplacingOccurrencesOfString:@"<body>"
                                                                      withString:@"<body></br>"];
            }
        }
        //NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
        [self.webview loadHTMLString:contentHtml baseURL:nil];
    }
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSString *) getData:(int)idDrug
{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"drugs.db"];
    NSString *result = @"";
    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];
    NSString *sqlSelectQuery = [NSString stringWithFormat:@"SELECT name,detail FROM drugs WHERE id= %d",idDrug];
    FMResultSet *resultsWithNameLocation = [database executeQuery:sqlSelectQuery];
    if (resultsWithNameLocation != nil) {
        [resultsWithNameLocation next];
        result = [NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"detail"]];
        [self setTitle:[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"name"]]];
    }
    [database close];
    return result;
}

- (UIBarButtonItem *) resourceBasedButton:(NSString *)resource withAction:(SEL)selector
{
	if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
	{
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:resource] forState:0];
        [btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
        if([@"ic_arrow_left" isEqualToString:resource])
        {
            btn.frame = CGRectMake(0, 0, 32, 32);
        }        else{
            btn.frame = CGRectMake(0, 0, 24, 24);
        }
        [btn setContentMode:UIViewContentModeScaleAspectFill];
        UIBarButtonItem *ib = [[UIBarButtonItem alloc] initWithCustomView:btn];
        return ib;
        
	}
	else
	{
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:resource] forState:0];
        [btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
        if([@"ic_arrow_left" isEqualToString:resource])
        {
            btn.frame = CGRectMake(0, 0, 32, 32);
        }
        else{
            btn.frame = CGRectMake(0, 0, 24, 24);
        }
        [btn setContentMode:UIViewContentModeScaleAspectFill];
        UIBarButtonItem *ib = [[UIBarButtonItem alloc] initWithCustomView:btn];
        return ib;
	}
}

- (void) onBack: (id)sender
{
   [[self navigationController] popViewControllerAnimated:YES];
}

@end
